package cn.edu.heuet.bnve.activity.ScheduleActivity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.model.Schedule;
import cn.edu.heuet.bnve.database.ScheduleDao;
import cn.edu.heuet.bnve.databinding.ActivityScheduleDetailBinding;
import cn.edu.heuet.bnve.utils.ColorUtil;
import cn.edu.heuet.bnve.utils.StringUtil;
import cn.edu.heuet.bnve.utils.XToastUtils;


public class ScheduleDetailActivity extends AppCompatActivity {

    private ActivityScheduleDetailBinding binding;
    private Schedule schedule;
    private ScheduleDao scheduleDao;

    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= DataBindingUtil.setContentView(this, R.layout.activity_schedule_detail);
        initData();
        initView();
        initEvent();
    }


    public static void show(Context context) {
        context.startActivity(new Intent(context, ScheduleDetailActivity.class));
    }

    public void initData() {

        Intent getIntent = getIntent();
        schedule = (Schedule) getIntent.getSerializableExtra("schedule");

    }

    public void initView() {
        //加载显示Schedule详情
        binding.detailAlarmTitle.setText(schedule.getTitle());
        binding.detailAlarmDate.setText(schedule.getYear()+"/"+schedule.getMonth()+"/"+schedule.getDay());
        binding.detailAlarmDescription.setText(schedule.getDescription());
        binding.detailAlarmLocal.setText(schedule.getLocal());
        binding.detailAlarmRemind.setText(StringUtil.getAlarmText(schedule.getAlarmTimeType()));
        if(schedule.getType()==0) {
            binding.detailAlarmStartEndTime.setText(String.format("%02d", schedule.getStartTimeHour()) + ":" + String.format("%02d", schedule.getStartTimeMinute()) +
                    "-" + String.format("%02d", schedule.getEndTimeHour()) + ":" + String.format("%02d", schedule.getEndTimeMinute()));
            binding.detailAlarmType.setText(R.string.text_schedule);
        }
        else {
            binding.detailAlarmStartEndTime.setText(getResources().getString(R.string.text_ddl)+": "+String.format("%02d", schedule.getEndTimeHour()) + ":" + String.format("%02d", schedule.getEndTimeMinute()));
            binding.detailAlarmType.setText(R.string.text_homework);
        }
        //这样设置可以保持圆角不变
        int color = getResources().getColor(ColorUtil.getBackgroundColor(schedule.getAlarmColor()));
        GradientDrawable gd = (GradientDrawable)binding.detailLayout.getBackground();
        gd.setColor(getResources().getColor(ColorUtil.getBackgroundColor(schedule.getAlarmColor())));
        gd.setAlpha(200);
        binding.detailLayout.setBackground(gd);
        binding.updateFab.setColorFilter(color);
        binding.iv1.setColorFilter(color);
        binding.iv2.setColorFilter(color);
        binding.iv3.setColorFilter(color);
        binding.iv4.setColorFilter(color);
        binding.iv5.setColorFilter(color);



        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.setStatusBarColor(getResources().getColor(ColorUtil.getBackgroundColor(schedule.getAlarmColor())));
        }

    }

    public void initEvent() {
        binding.icBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.updateFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                modifySchedule();
            }
        });

        binding.txDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDeleteDialog();
            }
        });
        binding.ivSheduleDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {showDeleteDialog ();}
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == 0) {
                if (data != null) {
                    schedule = (Schedule) data.getSerializableExtra("schedule");
                    initView();
                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
        else if (requestCode == 1) {
            if (resultCode == 0) {
                if (data != null) {
                    schedule = (Schedule) data.getSerializableExtra("schedule");
                    initView();
                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    /**
     * 修改Schedule，判断Type并打开相应的页面
     */
    private void modifySchedule(){
        if(schedule.getType()==0) {
            Intent intent = new Intent(ScheduleDetailActivity.this, ModifyScheduleActivity.class);
            intent.putExtra("schedule",schedule);
            startActivityForResult(intent, 0);
        }
        else{
            Intent intent = new Intent(ScheduleDetailActivity.this, ModifyHomeworkActivity.class);
            intent.putExtra("schedule",schedule);
            startActivityForResult(intent, 1);
        }
    }

    void showDeleteDialog(){
        final Dialog dialog = new Dialog(ScheduleDetailActivity.this, R.style.dialog_normal);
        dialog.create();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_delete_confirm);
        window.setWindowAnimations(R.style.dialog_style);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = getResources().getDisplayMetrics().widthPixels;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        TextView okButton = dialog.findViewById(R.id.button_ok);

        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scheduleDao = new ScheduleDao(ScheduleDetailActivity.this);
                scheduleDao.deleteDataById(schedule.getId());
                XToastUtils.success(R.string.text_delete_successful,1);
                finish();
                dialog.dismiss();
            }
        });
        TextView cancelButton = dialog.findViewById(R.id.button_cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        dialog.show();
    }
}